package com.gitee.huanminabc.utils_tools.security.vo;

import com.alibaba.fastjson.JSON;
import com.gitee.huanminabc.nullchain.common.NullResult;
import com.gitee.huanminabc.utils_tools.log_extend.LogTraceContextHolder;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dn
 * @since 2023/12/15
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SecurityResponse<T> extends NullResult<T> {

    static {
        //日志追踪id,用于追踪日志这个在web统一请求返回中补充
        TRACE_ID_FUN= LogTraceContextHolder::get;
    }

    public static void success(String t, HttpServletResponse response) throws IOException {
        NullResult<String> httpJsonResult = SecurityResponse.success(t, "成功");
        String jsonString = JSON.toJSONString(httpJsonResult);
        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(jsonString);
    }

    public static  void  error(Integer code, String message,HttpServletResponse response) throws IOException {
        NullResult<String> httpJsonResult = SecurityResponse.error(code, message);
        String jsonString = JSON.toJSONString(httpJsonResult);
        response.setStatus(500);
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.getWriter().print(jsonString);
    }
}
